package com.topscore.uke.mvp.model;

import com.topscore.uke.entities.PerformanceData;
import com.topscore.uke.entities.TeamPerformanceListEntity;
import com.topscore.uke.mvp.contractors.TeamPerformanceRankContractor;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetTeamSalesRequest;
import com.topscore.uke.network.retrofit.HandleErrorSubscriber;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 71468 on 2017/11/28.
 */

public class TeamPerformanceRankModel implements TeamPerformanceRankContractor.Model, TeamPerformanceRankContractor {
    private CallBack mCallBack;

    private GetTeamSalesRequest mRankRequest;
    private String mGroupCode;

    public TeamPerformanceRankModel(CallBack callBack, String groupCode) {
        this.mCallBack = callBack;
        this.mGroupCode = groupCode;
        mRankRequest = new GetTeamSalesRequest();
        mRankRequest.setShareUserGroupCode(groupCode);
        mRankRequest.setStartDay(getCurrentYearFirstMonth());
        mRankRequest.setEndDay(getCurrentMonth());
    }

    @Override
    public void getData() {
        mRankRequest.setSortName(mRankRequest._SORT_BY_MONEY);
        mRankRequest.setSortType(mRankRequest._SORT_DESC);
        Observable.merge(mService.getTeamPerformanceInYear(new SimpleDateFormat("YYYY").format(new Date()), mGroupCode),
                mService.getTeamSalesList(mRankRequest))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<?>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<?> response) {
                        if (response.getData() instanceof PerformanceData){
                            mCallBack.onGetPerformanceDataSuccess((BaseResponse<PerformanceData>) response);
                        }else if (response.getData() instanceof List){
                            mCallBack.onGetRankListSuccess((BaseResponse<List<TeamPerformanceListEntity>>) response);
                        }
                    }
                });
    }

    @Override
    public void getRankByAmount(String sortType) {
        mRankRequest.setSortName(mRankRequest._SORT_BY_MONEY);
        mRankRequest.setSortType(sortType);
        mService.getTeamSalesList(mRankRequest)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<List<TeamPerformanceListEntity>>>(mCallBack){
                    @Override
                    public void onSuccess(BaseResponse<List<TeamPerformanceListEntity>> response) {
                        mCallBack.onGetRankListSuccess(response);
                    }
                });
    }

    @Override
    public void getRankByCount(String sortType) {
        mRankRequest.setSortName(GetTeamSalesRequest._SORT_BY_COUNT);
        mRankRequest.setSortType(sortType);
        mService.getTeamSalesList(mRankRequest)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<List<TeamPerformanceListEntity>>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<List<TeamPerformanceListEntity>> response) {
                        mCallBack.onGetRankListSuccess(response);
                    }
                });
    }

    @Override
    public void getPerformanceByYear() {
        mService.getTeamPerformanceInYear(new SimpleDateFormat("YYYY").format(new Date()),mGroupCode)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<PerformanceData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<PerformanceData> response) {
                        mCallBack.onGetPerformanceDataSuccess(response);
                    }
                });
    }

    @Override
    public void getPerformanceByMonth() {
        mService.getTeamPerformanceInMonth(getCurrentMonth(),mGroupCode)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<PerformanceData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<PerformanceData> response) {
                        mCallBack.onGetPerformanceDataSuccess(response);
                    }
                });
    }

    @Override
    public void getPerformanceByYear(String year) {
        mService.getTeamPerformanceInYear(year,mGroupCode)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<PerformanceData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<PerformanceData> response) {
                        mCallBack.onGetPerformanceDataSuccess(response);
                    }
                });

    }

    @Override
    public void getPerformanceByMonth(String month) {
        mService.getTeamPerformanceInMonth(month,mGroupCode)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<PerformanceData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<PerformanceData> response) {
                        mCallBack.onGetPerformanceDataSuccess(response);
                    }
                });
    }

    public String getCurrentYearFirstMonth() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date date = calendar.getTime();
        String firstMonth = new SimpleDateFormat("YYYY-MM").format(date);
        return firstMonth;
    }

    public String getCurrentMonth() {
        Date date = new Date();
        String month = new SimpleDateFormat("YYYY-MM").format(date);
        return month;
    }
}
